﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace ThisIsTheOnlyChamber
{
    public abstract class Chamber : GameState
    {
        protected bool CanJump = true;
        protected string message;
        protected Player player;

        protected List<Platform> platforms = new List<Platform>();
        protected List<Wall> walls = new List<Wall>();
        protected List<Button> buttons = new List<Button>();
        protected Door door;

        protected SoundEffect ButtonSound;
        protected SoundEffect DoorOpenSound;

        protected MouseState lastMouseState;
        protected GamePadState currentGamepadState = GamePad.GetState(PlayerIndex.One);
        protected SpriteBatch sb;
        protected SpriteFont sf;

        protected void DrawChamber()
        {
            player.AspectRatio = GraphicsDevice.Viewport.AspectRatio;
            Matrix view = player.View;
            Matrix projection = player.Projection;

            foreach (Platform p in platforms)
            {
                if ( !(p is InvisiblePlatform) )
                    p.Draw(view, projection, GraphicsDevice);
            }

            foreach (Wall w in walls)
            {
                w.Draw(view, projection, GraphicsDevice);
            }

            foreach (Button b in buttons)
            {
                b.Draw(view, projection, GraphicsDevice);
            }

            if (door != null)
                door.Draw(view, projection, GraphicsDevice);

            sb = new SpriteBatch(Game.GraphicsDevice);
            sf = Game.Content.Load<SpriteFont>("MenuFont");

            sb.Begin();

            sb.DrawString(sf, message, new Vector2(5, 5), Color.White, 0, Vector2.Zero, .8f, SpriteEffects.None, 0);

            sb.End();

            GraphicsDevice.BlendState = BlendState.Opaque;
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
        }

        public override void ProcessGamePad(GamePadState gs)
        {
            player.LookRight((float)gs.ThumbSticks.Right.X * 0.1f);
            player.LookUp((float)gs.ThumbSticks.Right.Y * 0.1f);

            player.MoveForward(gs.ThumbSticks.Left.Y);
            player.Strafe(gs.ThumbSticks.Left.X);

            if (gs.IsButtonDown(Buttons.A) && CanJump)
            {
                    player.Jump();
                    CanJump = false;
            }
            if(!gs.IsButtonDown(Buttons.A))
            {
                CanJump = true;
            }
        }

        public override void ProcessKeyboardMouse(KeyboardState ks, MouseState ms)
        {
            if (!currentGamepadState.IsConnected)
            {
                if (lastMouseState != null)
                {
                    float xChange = ms.X - lastMouseState.X;
                    float yChange = ms.Y - lastMouseState.Y;

                    player.LookRight(xChange / 300);
                    player.LookUp(-yChange / 300);
                }
                if (ks.IsKeyDown(Keys.Up) || ks.IsKeyDown(Keys.W))
                {
                    player.MoveForward(1);
                }
                if (ks.IsKeyDown(Keys.Down) || ks.IsKeyDown(Keys.S))
                {
                    player.MoveForward(-1);
                }
                if (ks.IsKeyDown(Keys.Left) || ks.IsKeyDown(Keys.A))
                {
                    player.Strafe(-1);
                }
                if (ks.IsKeyDown(Keys.Right) || ks.IsKeyDown(Keys.D))
                {
                    player.Strafe(1);
                }
                if (ks.IsKeyDown(Keys.Space) && CanJump)
                {
                    player.Jump();
                    CanJump = false;
                }
                if (ks.IsKeyUp(Keys.Space))
                {
                    CanJump = true;
                }
                lastMouseState = ms;
            }
        }

        public Chamber(Game g, Vector3 startingPosition, string s)
            : base(g)
        {
            message = s;
            player = new Player(startingPosition, 66);
        }
    }
}
